package com.xyh.jdk8Demo;

import javax.sound.midi.Soundbank;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @version v1.0
 * @类描述：
 * @项目名称：
 * @包名： com.xyh.jdk8Demo
 * @类名称：StreamDemo
 * @创建人：YIHANG.XU
 * @创建时间：2020/9/204:33 下午
 * @修改人：YIHANG.XU
 * @修改时间：2020/9/204:33 下午
 * @修改备注：
 * @Copyright
 */
public class StreamDemo {
    public static void main(String[] args) {
        List<Student> above90List = new ArrayList<>();
        Student student1 = new Student();
        student1.setScore(92.32D);
        student1.setName("小明");
        Student student2 = new Student();
        student2.setScore(91.32D);
        student2.setName("小张");
        Student student3 = new Student();
        student3.setScore(99.32D);
        student3.setName("小花");
        Student student4 = new Student();
        student4.setScore(86.32D);
        student4.setName("张三");
        Student student5 = new Student();
        student5.setScore(33.32D);
        student5.setName("李四");
        Student student6 = new Student();
        student6.setScore(49.32D);
        student6.setName("王五");
        Student student7 = new Student();
        student7.setScore(99.32D);
        student7.setName("王五");
        above90List.add(student1);
        above90List.add(student2);
        above90List.add(student3);
        above90List.add(student4);
        above90List.add(student5);
        above90List.add(student6);
        above90List.add(student7);
        // 进行流程处理
        // 流式编程 一共连个 流对象
        // stream 顺序流 单线程模式   parallelStream 并行流 多线程模式
        //                                           过滤                                返回一个新的集合
        List<Student> collect = above90List.stream().filter(t -> t.getScore() > 90).collect(Collectors.toList());
        for (Student student : collect) {
            System.out.println(student.getScore());
        }
        System.out.println("++++++++++++++++++++");
        //map 添加创建
        List<String> collect1 = above90List.stream().filter(t -> t.getScore() > 20).map(Student::getName).collect(Collectors.toList());
        for (String s : collect1) {
            System.out.println(s);
        }
        System.out.println("++++++++++++++++++++");
        //根据map的字段进行去重
        List<String> collect2 = above90List.stream().filter(t -> t.getScore() > 90).map(Student::getName).distinct().collect(Collectors.toList());
        for (String s : collect2) {
            System.out.println(s);
        }
        System.out.println("++++++++++++++++++++");
        //sorted 排序方法
        List<Student> collect3 = above90List.stream().filter(t -> t.getScore() > 90).sorted(Comparator.comparing(Student::getScore)).collect(Collectors.toList());
        for (Student student : collect3) {
            System.out.println(student.getScore() + "-----" + student.getName());
        }
        System.out.println("++++++++++++++++++++");
        //reversed 排序倒转
        List<Student> list = above90List.stream().filter(t -> t.getScore() > 90).sorted(Comparator.comparing(Student::getScore).
                reversed().thenComparing(Student::getName))
                .collect(Collectors.toList());
        for (Student student : list) {
            System.out.println(student.getScore() + "-----" + student.getName());
        }

    }

}
